﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Enterprise.DataFactory;
using Enterprise.Infrastructure;
using Enterprise.Model.Article;
namespace Enterprise.Data.Article
{
    /// <summary>
    ///  通知公告数据操作
    /// </summary>
    public class ArticleData
    {
        /// <summary>
        /// 通知公告内容显示
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public ArticleEntity ArticleDisp(int articleId)
        {
            string cmd = "Select * from ARTICLE_INFO where ARTICLEID=" + articleId;
            IDataReader dr = DbExecute.DataReader(cmd);
            return FormatArticleList(dr).FirstOrDefault();
        }

        /// <summary>
        ///  通知公告数据列表
        /// </summary>
        /// <returns></returns>
        public List<ArticleEntity> ArticleList(int moduleId)
        {
            string cmd = "Select * from ARTICLE_INFO where AMODULEID=" + moduleId;
            IDataReader dr = DbExecute.DataReader(cmd);           
            return FormatArticleList(dr);
        }
        /// <summary>
        ///  通知公告数据列表
        /// </summary>
        /// <returns></returns>
        public List<ArticleEntity> ArticleList(string cmd)
        {
            List<ArticleEntity> list = new List<ArticleEntity>();
            IDataReader dr = DbExecute.DataReader(cmd);            
            return FormatArticleList(dr);
        }

        /// <summary>
        /// 通知公告数据操作方法
        /// </summary>
        /// <param name="cmd"></param>
        public void ArticleExecute(string cmd)
        {
            DbExecute.Execute(cmd);
        }

        /// <summary>
        /// 通知公告数据操作方法
        /// </summary>
        /// <param name="cmd"></param>
        public void ArticleExecute(string cmd,string item1,object item1Value,string item2,object item2Value)
        {
            DbExecute.ExecuteCommand(cmd, item1, item1Value, item2, item2Value);
        }

        /// <summary>
        /// 提取通用转换数据代码
        /// 格式化通知公告
        /// </summary>
        /// <param name="dr">数据集</param>
        /// <returns></returns>
        private List<ArticleEntity> FormatArticleList(IDataReader dr)
        {
            List<ArticleEntity> list = new List<ArticleEntity>();
            while (dr.Read())
            {
                ArticleEntity entity = new ArticleEntity();
                if (!Convert.IsDBNull(dr["ARTICLEID"])) entity.ArticleId = Convert.ToInt32(dr["ARTICLEID"]);
                if (!Convert.IsDBNull(dr["AMODULEID"])) entity.AModuleId = Convert.ToInt32(dr["AMODULEID"]);
                if (!Convert.IsDBNull(dr["ADEPARTMENT"])) entity.ADepartMent = Convert.ToInt32(dr["ADEPARTMENT"]);
                if (!Convert.IsDBNull(dr["AUSER"])) entity.AUser = Convert.ToInt32(dr["AUSER"]);
                if (!Convert.IsDBNull(dr["ATYPE"])) entity.AType = Convert.ToString(dr["ATYPE"]);
                if (!Convert.IsDBNull(dr["ATITLE"])) entity.ATitle = Convert.ToString(dr["ATITLE"]);
                if (!Convert.IsDBNull(dr["ATITLERU"])) entity.ATitleRu = Convert.ToString(dr["ATITLERU"]);
                if (!Convert.IsDBNull(dr["ACONTENT"])) entity.AContent = Convert.ToString(dr["ACONTENT"]);
                if (!Convert.IsDBNull(dr["ACONTENTRU"])) entity.AContentRu = Convert.ToString(dr["ACONTENTRU"]);
                if (!Convert.IsDBNull(dr["ARELEASETIME"])) entity.AReleaseTime = Convert.ToDateTime(dr["ARELEASETIME"]);
                if (!Convert.IsDBNull(dr["AINVALIDTIME"])) entity.AInvalidTime = Convert.ToDateTime(dr["AINVALIDTIME"]);
                if (!Convert.IsDBNull(dr["ARECEVIEUSER"])) entity.ARecevieUser = Convert.ToString(dr["ARECEVIEUSER"]);
                if (!Convert.IsDBNull(dr["ACREATEUSER"])) entity.ACreateUser = Convert.ToInt32(dr["ACREATEUSER"]);
                if (!Convert.IsDBNull(dr["ACREATETIME"])) entity.ACreateTime = Convert.ToDateTime(dr["ACREATETIME"]);
                list.Add(entity);
            }
            dr.Close();
            return list;
        }

    }
}
